﻿using SharpErp.Data.Common.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<GeneralInfoItem> GetSysGeneralInfoItem(string key)
        {
            var list = GetAllKvItemClass<GeneralInfoItem>(key);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddGeneralInfoItem(string key, GeneralInfoItem item)
        {
            var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, key);
        }

        public void UpdateGeneralInfoItem(string key, GeneralInfoItem item)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(items, key);
            }
        }

        public void DeleteGeneralInfoItem(string key, int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(key);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.GeneralInfoItem>(items, key);
            }
        }

        public List<FormulationItem> GetSysFormulationItem(string key)
        {
            var list = GetAllKvItemClass<FormulationItem>(key);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList(); ;
        }

        public void AddFormulationItem(string key, FormulationItem item)
        {
            var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
            var maxId = 1;
            if (items != null && items.Any()) maxId = (from p in items select p.Id).Max() + 1;
            item.Id = maxId;
            item.CreateTime = item.LastModifyTime = new DateTime();
            if (items == null) return;
            items.Add(item);
            SetAllKvItemClass(items, key);
        }

        public void UpdateFormulationItem(string key, FormulationItem item)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
                for (var i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id != item.Id) continue;
                    items[i].LastModifyTime = new DateTime();
                    items[i].Name = item.Name;
                    items[i].Formulation = item.Formulation;
                    break;
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(items, key);
            };
        }

        public void DeleteFormulationItem(string key, int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var items = GetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(key);
                for (int i = 0; i < items.Count(); ++i)
                {
                    if (items[i].Id == id)
                    {
                        items[i].LastModifyTime = new DateTime();
                        items[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvItemClass<SharpErp.Data.Common.Entity.FormulationItem>(items, key);
            }
        }
    }
}
